package com.oocl.products.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oocl.products.dao.ProductDao;
import com.oocl.products.pojo.Product;
import com.oocl.products.util.DBHelper;

public class ProductDaoImpl implements ProductDao {

	public int addProduct(Product product) {
		String sql = "insert into product(pid, name,price,total,pd,sid) values(?,?,?,?,?,?)";
		Connection conn = DBHelper.getConnectionWithDs();
		PreparedStatement pst = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, product.getPid());
			pst.setString(2, product.getName());
			pst.setDouble(3, product.getPrice());
			pst.setInt(4, product.getTotal());
			pst.setDate(5, new java.sql.Date(product.getPd().getTime()));
			pst.setInt(6, product.getSid());
			return pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, null, conn);
		}
		return -1;
	}

	public int deleteProduct(String pid) {
		String sql = "delete from product where pid = ? ";
		Connection conn = DBHelper.getConnectionWithDs();
		PreparedStatement pst = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, pid);
			return pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, null, conn);
		}
		return -1;
	}
	
	public int updateProduct(Product product) {
		String sql = "update product set name = ?, price = ?, total=?, pd=?,sid=? where pid=? ";
		Connection conn = DBHelper.getConnectionWithDs();
		PreparedStatement pst = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, product.getName());
			pst.setDouble(2, product.getPrice());
			pst.setInt(3, product.getTotal());
			pst.setDate(4, new java.sql.Date(product.getPd().getTime()));
			pst.setInt(5, product.getSid());
			pst.setString(6, product.getPid());
			return pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, null, conn);
		}
		return -1;
	}
	
	public Product queryProductByPid(String pid) {
		Connection conn = DBHelper.getConnectionWithDs();
		Product product = null;
		String sql = "select * from product where pid = ? ";
		PreparedStatement pst = null;
		ResultSet rst = null;
		try {
			pst =  conn.prepareStatement(sql);
			pst.setString(1, pid);
			rst = pst.executeQuery();
		
			while(rst.next()){
				product = new Product();
				product.setPid(rst.getString(1));
				product.setName(rst.getString(2));
				product.setPrice(rst.getDouble(3));
				product.setTotal(rst.getInt(4));
				product.setPd(new Date(rst.getDate(5).getTime()));
				product.setSid(rst.getInt(6));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, rst, conn);
		}
		return product;
	}

	public List<Product> queryAllProductBySid(Integer sid) {
		Connection conn = DBHelper.getConnectionWithDs();
		List<Product> list = new ArrayList<Product>();
		Product product = null;
		String sql = "select * from product where sid = ? ";
		PreparedStatement pst = null;
		ResultSet rst = null;
		try {
			pst =  conn.prepareStatement(sql);
			pst.setInt(1, sid);
			rst = pst.executeQuery();
		
			while(rst.next()){
				product = new Product();
				product.setPid(rst.getString(1));
				product.setName(rst.getString(2));
				product.setPrice(rst.getDouble(3));
				product.setTotal(rst.getInt(4));
				product.setPd(new Date(rst.getDate(5).getTime()));
				product.setSid(rst.getInt(6));
				list.add(product);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, rst, conn);
		}
		return list;
	}

}
